package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
/**
 * @author 黄衡熙
 * @version 1.0
 */
@WebServlet("/findAll")
public class EmpListServlet extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html lang=\"en\">");
        out.println("<head>");
        out.println("    <meta charset=\"UTF-8\">");
        out.println("    <title>员工列表</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>员工列表</h1>");
        out.println("<a href=\"showAddEmp\">查询员工</a>");
        out.println("<hr>");
        out.println("<table border=\"1\">");
        out.println("    <tr>");
        out.println("        <th>编号</th>");
        out.println("        <th>姓名</th>");
        out.println("        <th>工资</th>");
        out.println("        <th>操作</th>");
        out.println("    </tr>");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8","root","qq670892");
            String sql="select id,name,salary from t_emp";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while(rs.next()){
                int id=rs.getInt("id");
                String name=rs.getString("name");
                double salary=rs.getDouble("salary");
                out.println("<tr>");
                out.println("<td>");
                out.println(id);
                out.println("</td>");
                out.println("<td>");
                out.println(name);
                out.println("</td>");
                out.println("<td>");
                out.println(salary);
                out.println("</td>");
                out.println("<td>");
                out.println("<a href=\"findById?id="+id+"\">修改</a>");
                out.println("<a href=\"deleteById?id="+id+"\" onclick=\"confirm('是否确认删除?');\">删除</a>");
                out.println("</td>");
                out.println("</tr>");
                out.println("<br>");
            }
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
        } catch (ClassNotFoundException e) {
            out.println("<h1 style='color:red;'>注册驱动失败，失败原因:"+e.getMessage()+"</h1>");
        } catch (SQLException e) {
            out.println("<h1 style='color:red;'>获取连接失败，失败原因:"+e.getMessage()+"</h1>");
        }finally {
            if(conn !=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    out.println("<h1 style='color:red;'>释放连接失败，失败原因:"+e.getMessage()+"</h1>");
                }
            }
            if(ps !=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    out.println("<h1 style='color:red;'>释放连接失败，失败原因:"+e.getMessage()+"</h1>");
                }
            }
            if(rs !=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    out.println("<h1 style='color:red;'>释放连接失败，失败原因:"+e.getMessage()+"</h1>");
                }
            }
        }

    }
}
